package com.lp.controller.iot;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lp.bo.IotSceneUserRelationBO;
import com.lp.bo.UserInfoBO;
import com.lp.common.Code;
import com.lp.common.Constants;
import com.lp.common.RequestURLIOT;
import com.lp.controller.BaseController;
import com.lp.util.ObjectUtil;
import com.lp.util.ResultMapUtils;


@Controller
public class IotSceneUserRelationController extends BaseController {

	/**
	 * 检索
	 * @param response
	 * @param 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.SceneUserRelation.SCENE_USER_RELATION_PAGE)
	public ModelAndView selectPage(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotSceneUserRelationBO obj,
			@RequestParam(required=false) Integer pageSize ,
			@RequestParam Integer paged ) {
		Map<String, Object> resultMap = getResultMap();
		try {

			resultMap = service.selectPageList("IotSceneUserRelation.selectPage",getPageBean(paged,pageSize), obj);				

		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	//SCENE_USER_RELATION_CHANGE
	/**
	 * 切换场景信息
	 * @param response
	 * @param userKey
	 * @param obj
	 * @return
	 */
	@RequestMapping(method = RequestMethod.PUT, value = RequestURLIOT.SceneUserRelation.SCENE_USER_RELATION_CHANGE )
	public ModelAndView changeScene(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotSceneUserRelationBO obj ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user2 = getUserInfoByUserKey(obj.getUserKey());
			if(ObjectUtil.isNotEmpty(user2)){
				UserInfoBO user = getUserInfoByUserKey(userKey);
				obj.setUser_id(user.getId());
				// 删除当前用户授权信息
				service.delete("IotSceneUserRelation.deleteUserSceneRelation", obj);
			
			// 添加新用户的授权
				obj.setUser_id(user2.getId());
				service.insert("IotSceneUserRelation.insert", obj);
			}else{
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.NO_DATA);
			}
		
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 插入
	 * @param response
	 * @param 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.SceneUserRelation.SCENE_USER_RELATION )
	public ModelAndView save(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotSceneUserRelationBO obj ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user = getUserInfoByUserKey(userKey);
			obj.setAid(user.getId());
			if(ObjectUtil.isNotEmpty(obj.getIds())){
				String[] ids = obj.getIds().split(",");
				// 删除当前用户的全部授权
				service.delete("IotSceneUserRelation.deleteUserRelation", obj);
				for(int i=0;i<ids.length;i++){
					obj.setScene_id(Integer.parseInt(ids[i]));
					resultMap = service.insert("IotSceneUserRelation.insert", obj) ;	
				}
			}else {
				if(ObjectUtil.isNotEmpty(obj.getScene_id())){
					resultMap = service.insert("IotSceneUserRelation.insert", obj) ;					
				}else{
					// 不授权场景给某个用户
					// 删除当前用户的全部授权
					service.delete("IotSceneUserRelation.deleteUserRelation", obj);
				}
			}
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 查询单个
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = RequestURLIOT.SceneUserRelation.SCENE_USER_RELATION )
	public ModelAndView selectOne(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestParam Integer id ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			
			resultMap = service.selectOne("IotSceneUserRelation.selectOne", new IotSceneUserRelationBO(id)) ;
		} catch (Exception e) {
			exception(e,resultMap, id);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 更新
	 * @param response
	 * @param obj
	 * @return
	 */
	@RequestMapping(method = RequestMethod.PUT, value = RequestURLIOT.SceneUserRelation.SCENE_USER_RELATION )
	public ModelAndView update(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotSceneUserRelationBO obj ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			resultMap = service.update("IotSceneUserRelation.update", obj) ;
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 删除
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.DELETE, value = RequestURLIOT.SceneUserRelation.SCENE_USER_RELATION )
	public ModelAndView delete(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestParam Integer id ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			IotSceneUserRelationBO obj = new IotSceneUserRelationBO();
			if(ObjectUtil.isEmpty(id)){
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.PARAM_ERROR);
			}else{
				obj.setId(id);
				obj.setDelete_flag(Constants.DELETE.YES);
				resultMap = service.update("IotSceneUserRelation.update", obj) ;		
			}
		} catch (Exception e) {
			exception(e,resultMap, id);
		}
		return getModelAndView(response, resultMap);
	}
}
